package com.thd.springboottest.mybatis.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.executor.Executor;

import java.util.Properties;

/**
 * com.thd.springboottest.mybatis.interceptor.MyResultInterceptor
 *
 * @author: wanglei62
 * @DATE: 2022/4/29 11:38
 **/
@Intercepts({@Signature(type = Executor.class, method = "query", args = {
        MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class}),
        @Signature(type = Executor.class, method = "query", args = {
                MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class})
})
public class MyResultInterceptor implements Interceptor {
    public Object intercept(Invocation invocation)
            throws Throwable
    {
        Object result = invocation.proceed(); // 执行请求方法，并将所得结果保存到result中
        String str = new ObjectMapper().writeValueAsString(result);
        System.out.println("============= resultSet：" + str);
        return result;
    }

    public Object plugin(Object target)
    {
        return Plugin.wrap(target, this);
    }

    public void setProperties(Properties arg0)
    {}
}
